<template>
  <el-dialog
    :model-value="visible"
    @update:model-value="$emit('update:visible', $event)"
    title="投资详情"
    width="700px"
  >
    <div class="investment-detail-content" v-if="data && Object.keys(data).length > 0">
      <!-- 基本信息 -->
      <div class="detail-section">
        <h4>基本信息</h4>
        <div class="info-grid">
          <div class="info-item">
            <span class="label">产品名称：</span>
            <span class="value">{{ data.productName || '暂无数据' }}</span>
          </div>
          <div class="info-item">
            <span class="label">投资金额：</span>
            <span class="value">¥{{ formatAmount(data.amount) }}</span>
          </div>
          <div class="info-item">
            <span class="label">当前价值：</span>
            <span class="value">¥{{ formatAmount(data.currentValue) }}</span>
          </div>
          <div class="info-item">
            <span class="label">盈亏金额：</span>
            <span class="value" :class="getProfitClass(data.profit)">
              {{ formatAmount(data.profit, true) }}
            </span>
          </div>
          <div class="info-item">
            <span class="label">投资日期：</span>
            <span class="value">{{ formatDate(data.createTime) || '暂无数据' }}</span>
          </div>
          <div class="info-item">
            <span class="label">到期日期：</span>
            <span class="value">{{ formatDate(data.maturityDate) || '暂无数据' }}</span>
          </div>
          <div class="info-item">
            <span class="label">订单状态：</span>
            <span class="value" v-if="data.status !== undefined">
              <el-tag :type="getOrderStatusType(data.status)">
                {{ getOrderStatusText(data.status) }}
              </el-tag>
            </span>
            <span class="value" v-else>暂无数据</span>
          </div>
        </div>
      </div>

      <!-- 收益信息 -->
      <div class="detail-section" v-if="data.expectedReturn !== undefined && data.expectedReturn !== null">
        <h4>收益信息</h4>
        <div class="info-grid">
          <div class="info-item">
            <span class="label">预期年化收益率：</span>
            <span class="value highlight">{{ formatRate(data.expectedReturn) }}%</span>
          </div>
          <div class="info-item">
            <span class="label">投资期限：</span>
            <span class="value">{{ data.duration || 0 }}天</span>
          </div>
          <div class="info-item">
            <span class="label">预期收益：</span>
            <span class="value positive">¥{{ formatAmount(data.expectedProfit) }}</span>
          </div>
          <div class="info-item">
            <span class="label">已产生收益：</span>
            <span class="value" :class="getProfitClass(data.accumulatedProfit)">
              ¥{{ formatAmount(data.accumulatedProfit, true) }}
            </span>
          </div>
        </div>
      </div>

      <!-- 交易记录 -->
      <div class="detail-section" v-if="data.transactions && data.transactions.length > 0">
        <h4>交易记录</h4>
        <div class="transaction-list">
          <div class="transaction-item" v-for="transaction in data.transactions" :key="transaction.id">
            <div class="transaction-info">
              <span class="transaction-type">{{ getTransactionTypeText(transaction.type) }}</span>
              <span class="transaction-amount" :class="getAmountClass(transaction.type)">
                {{ formatTransactionAmount(transaction.amount, transaction.type) }}
              </span>
            </div>
            <div class="transaction-time">
              {{ formatDate(transaction.createTime) }} {{ formatTime(transaction.createTime) }}
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="detail-placeholder" v-else>
      <el-icon class="placeholder-icon"><Document /></el-icon>
      <p>暂无详细信息</p>
      <p class="placeholder-tip">可能的原因：</p>
      <ul class="placeholder-list">
        <li>该投资订单数据不完整</li>
        <li>服务器返回数据为空</li>
        <li>数据正在加载中</li>
      </ul>
      <el-button size="small" @click="$emit('update:visible', false)">关闭</el-button>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="$emit('update:visible', false)">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { Document } from '@element-plus/icons-vue';
import { formatAmount, formatDate, getOrderStatusText, getOrderStatusType } from './utils';

defineProps({
  visible: {
    type: Boolean,
    required: true
  },
  data: {
    type: Object,
    default: null
  }
});

defineEmits(['update:visible']);

// 工具函数
const formatRate = (rate) => {
  if (rate === undefined || rate === null) return '0.00';
  return parseFloat(rate).toFixed(2);
};

const formatTime = (dateTime) => {
  if (!dateTime) return '';
  const date = new Date(dateTime);
  return date.toLocaleTimeString('zh-CN', { hour12: false });
};

const getProfitClass = (profit) => {
  if (!profit) return '';
  return parseFloat(profit) >= 0 ? 'positive' : 'negative';
};

const getTransactionTypeText = (type) => {
  const typeMap = {
    1: '投资',
    2: '赎回',
    3: '分红',
    4: '手续费'
  };
  return typeMap[type] || '未知';
};

const getAmountClass = (type) => {
  // 1: 投资(支出), 2: 赎回(收入), 3: 分红(收入), 4: 手续费(支出)
  return type === 2 || type === 3 ? 'positive' : 'negative';
};

const formatTransactionAmount = (amount, type) => {
  const sign = (type === 2 || type === 3) ? '+' : '-';
  return `${sign}¥${formatAmount(Math.abs(amount))}`;
};
</script>

<style scoped>
.investment-detail-content {
  padding: 8px;
}

.detail-section {
  margin-bottom: 24px;
}

.detail-section h4 {
  margin: 0 0 16px 0;
  font-size: 16px;
  color: #303133;
  font-weight: 600;
  border-bottom: 1px solid #ebeef5;
  padding-bottom: 8px;
}

.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid #f5f7fa;
}

.label {
  color: #909399;
  font-size: 14px;
}

.value {
  color: #303133;
  font-weight: 500;
  font-size: 14px;
}

.value.highlight {
  color: #e6a23c;
  font-weight: 600;
}

.positive {
  color: #67c23a;
}

.negative {
  color: #f56c6c;
}

/* 交易记录样式 */
.transaction-list {
  max-height: 200px;
  overflow-y: auto;
}

.transaction-item {
  padding: 12px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  margin-bottom: 8px;
  background-color: #fafafa;
}

.transaction-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 4px;
}

.transaction-type {
  font-weight: 500;
  color: #303133;
}

.transaction-amount {
  font-weight: 600;
}

.transaction-time {
  font-size: 12px;
  color: #909399;
}

/* 占位符样式 */
.detail-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
  text-align: center;
}

.placeholder-icon {
  font-size: 48px;
  color: #909399;
  margin-bottom: 16px;
}

.detail-placeholder p {
  margin: 8px 0;
  color: #606266;
}

.placeholder-tip {
  margin-top: 16px;
  font-weight: 500;
  color: #606266;
}

.placeholder-list {
  text-align: left;
  color: #909399;
  margin: 8px 0 20px 0;
  padding-left: 20px;
}

.placeholder-list li {
  margin-bottom: 4px;
}

.dialog-footer {
  text-align: right;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .info-grid {
    grid-template-columns: 1fr;
  }
}
</style>
